/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.alvin.api.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author tangzhichao
 */
public class StringTabVarPane extends javax.swing.JPanel {

    /**
     * Creates new form StringTabVarPane
     */
    public StringTabVarPane() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        inputText = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        outputText = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        lianxvText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        feilianxvText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        noTirmBox = new javax.swing.JRadioButton();
        trimStringBox = new javax.swing.JRadioButton();
        replaceBox = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();

        inputText.setColumns(20);
        inputText.setRows(5);
        jScrollPane1.setViewportView(inputText);

        jTabbedPane1.addTab("输入字符串", jScrollPane1);

        jButton1.setText("连续列");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("不连续");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        outputText.setColumns(20);
        outputText.setRows(5);
        jScrollPane2.setViewportView(outputText);

        jTabbedPane2.addTab("输出字符串", jScrollPane2);

        jLabel1.setText("(例子: 1-5)：");

        jLabel2.setText("(例子 1,3,4)：");

        jLabel3.setText("分列方式：");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "制表符", "空格" }));

        buttonGroup1.add(noTirmBox);
        noTirmBox.setSelected(true);
        noTirmBox.setText("不除空格");

        buttonGroup1.add(trimStringBox);
        trimStringBox.setText("去除两边空格");

        buttonGroup1.add(replaceBox);
        replaceBox.setText("去除所有空格");

        jLabel4.setText("列的空格处理：");

        jLabel5.setText("(例子：1)");

        jButton3.setText("单列");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lianxvText, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(feilianxvText, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(51, 51, 51)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(noTirmBox)
                        .addGap(18, 18, 18)
                        .addComponent(trimStringBox)
                        .addGap(18, 18, 18)
                        .addComponent(replaceBox)
                        .addGap(0, 106, Short.MAX_VALUE))
                    .addComponent(jTabbedPane1)
                    .addComponent(jTabbedPane2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(noTirmBox)
                    .addComponent(trimStringBox)
                    .addComponent(replaceBox)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jLabel1)
                    .addComponent(lianxvText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(feilianxvText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(18, 18, 18)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.outputText.setText("");
        int[] lianXv = getLianXv();
        if (lianXv == null) {
            return;
        }
        //连续列
        String splitStr = getSplitStr();
        String[] array = this.inputText.getText().trim().split("\r|\n|\r\n");
        for (String s : array) {
            String[] columns = s.split(splitStr);
            String line = "";
            for (int i = lianXv[0]; i <= lianXv[1]; i++) {
                String c = columns[i];
                if (trimStringBox.isSelected()) {
                    c = c.trim();
                } else if (replaceBox.isSelected()) {
                    c = c.replaceAll("\\s", "");
                }
                line += c + "\t";
            }
            line = line.trim();
            this.outputText.append(line + System.lineSeparator());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.outputText.setText("");
        int[] indexArray = getFeiLianXv();
        if (indexArray == null) {
            return;
        }
        //连续列
        String splitStr = getSplitStr();
        String[] array = this.inputText.getText().trim().split("\r|\n|\r\n");
        for (String s : array) {
            String[] columns = s.split(splitStr);
            String line = "";
            for (int i = 0; i < indexArray.length; i++) {
                String c = columns[indexArray[i]];
                if (trimStringBox.isSelected()) {
                    c = c.trim();
                } else if (replaceBox.isSelected()) {
                    c = c.replaceAll("\\s", "");
                }
                line += c + "\t";
            }
            line = line.trim();
            this.outputText.append(line + System.lineSeparator());
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       this.outputText.setText("");
       int index = Integer.parseInt(this.jTextField1.getText().trim()) -1;
        //连续列
        String splitStr = getSplitStr();
        String[] array = this.inputText.getText().trim().split("\r|\n|\r\n");
        for (String s : array) {
            String[] columns = s.split(splitStr);
             String c = columns[index];
                if (trimStringBox.isSelected()) {
                    c = c.trim();
                } else if (replaceBox.isSelected()) {
                    c = c.replaceAll("\\s", "");
                }
            this.outputText.append(c + System.lineSeparator());
        }
    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField feilianxvText;
    private javax.swing.JTextArea inputText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField lianxvText;
    private javax.swing.JRadioButton noTirmBox;
    private javax.swing.JTextArea outputText;
    private javax.swing.JRadioButton replaceBox;
    private javax.swing.JRadioButton trimStringBox;
    // End of variables declaration//GEN-END:variables

    private String getSplitStr() {
        if (this.jComboBox1.getSelectedIndex() == 0) {
            return "\t";
        }
        return "\\s+";
    }

    private int[] getLianXv() {
        String regex = "(\\d+)[^\\d]+(\\d+)";
        String text = this.lianxvText.getText().trim();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text);
        if (!m.find()) {
            return null;
        }
        int start = Integer.parseInt(m.group(1));
        int end = Integer.parseInt(m.group(2));

        if (start > end) {
            start ^= end;
            end ^= start;
            start ^= end;
        }
        return new int[]{start - 1, end - 1};
    }

    private int[] getFeiLianXv() {
        String regex = "(\\d+)";
        String text = this.feilianxvText.getText().trim();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text);
        List<Integer> list = new ArrayList<Integer>();
        while (m.find()) {
            int i = Integer.parseInt(m.group(1)) - 1;
            list.add(i);
        }
        int[] array = new int[list.size()];
        int index = 0;
        for (Integer i : list) {
            array[index++] = i;
        }
        return array;
    }
}
